
import rpyc
from dexen.system.libs import _constants

from copy import deepcopy

class ClientAPI:
    def __init__(self, url):
        self.conn = rpyc.connect(url, port = _constants._CLIENT_API_PORT)
        
    def _getLocalCopy(self, remote_copy):
        local_copy = []
        for item in remote_copy:
            local_copy.append(item)
        return local_copy
            
    def setSchema(self, schema_name, zip_data, last_modified_time):
        self.conn.root.setSchema(schema_name, zip_data, last_modified_time)
      
    def getSchema(self, schema_name):
        return self.conn.root.getSchema(schema_name)
        
    def getSchemaNames(self):
         return self._getLocalCopy(self.conn.root.getSchemaNames())
    
    def getSchemaTimestamp(self, schema_name):
        return self.conn.root.getSchemaTimestamp(schema_name)
    
    def setPopulation(self, schema_name, pop_name):
        self.conn.root.setPopulation(schema_name, pop_name)

    def getPopulationNames(self, schema_name):
        return self._getLocalCopy(self.conn.root.getPopulationNames(schema_name))
    
    def getScriptNames(self, schema_name, pop_name):
        return self._getLocalCopy(self.conn.root.getScriptNames(schema_name, pop_name))

    def getScriptFileFunctionNames(self, schema_name, pop_name, script_name):
        return self._getLocalCopy(self.conn.root.getScriptFileFunctionNames(schema_name, pop_name, script_name))    
    
    def setIndividualTable(self, schema_name, pop_name):
        self.conn.root.setIndividualTable(schema_name, pop_name)   
        
class ManagerAPI:
    def __init__(self, url, schema_name, pop_name):
        self.url = url
        self.schema_name = schema_name
        self.pop_name = pop_name
        self.conn = rpyc.connect(url, _constants._MANAGER_API_PORT)
        self.conn.root.setRPYCObj(schema_name, pop_name)
        
    def hasPopulationStopped(self):
        return self.conn.root.hasPopulationStopped()        